import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/home'
  }, {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    beforeEnter(to, from, next) {
      const { isLogin } = localStorage
      isLogin ? next('/home') : next()      
    }
  }, {
    path: '/home',
    component: () => import('@/views/home/index.vue')
  }, {
    path: '/cart',
    component: () => import('@/views/cart/index.vue')
  }, {
    path: '/order',
    component: () => import('@/views/order/index.vue')
  }, {
    path: '/my',
    component: () => import('@/views/my/index.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to, from)
  const isLogin = localStorage.isLogin
  if (isLogin || to.path == '/login') {
    next()
  } else {
    next('/login')
  }
})


export default router
